package cn.szzsi.model;

import cn.szzsi.common.util.CommonUtil;
import cn.szzsi.model.base.BaseRoleResource;
import com.jfinal.core.JFinal;
import com.jfinal.kit.StrKit;

import java.io.File;
import java.util.List;

/**
 * 实体类 - 角色和资源的映射
 * 
 */
//@TableBind(tableName = "b_role_resource")
public class RoleResource extends BaseRoleResource<RoleResource> {

	private static final long serialVersionUID = 4675418211373785326L;
	
	public static final RoleResource dao = new RoleResource().dao();

	public List<RoleResource> getAll(){
		return dao.find("select * from sys_role_resource where uid='"+ CommonUtil.PLATFROM_FLAG+"'");
	}

	public RoleResource getByRoleAndResourceId(String roleId, String resourceId){
		return dao.findFirst("select * from sys_role_resource where roleId=? and resourceId = ?",roleId,resourceId);
	}

	public void deleteByRoleAndResourceId(String roleId, String resourceId){
		List<RoleResource> lbrr = dao.find("select * from sys_role_resource where roleId=? and resourceId = ?",roleId,resourceId);
		if(lbrr!=null && lbrr.size()>0){
			for(RoleResource brr:lbrr){
				brr.delete();
			}
		}

	}

	public List<RoleResource> getByRoleId(String roleId){
		return dao.find("select * from sys_role_resource where roleId = ?",roleId);
	}

	public List<RoleResource> getByUid(String uid){
		return dao.find("select * from sys_role_resource where uid=?",uid);
	}

	//资源和权限是否对应在一起
	public boolean isRoleResourceRalationExsit(String rid, String roleId){
		List<RoleResource> lbrr = RoleResource.dao.find("select * from sys_role_resource where roleId = ? and resourceId = ?",roleId,rid);
		if(lbrr!=null && lbrr.size()>0){
			return true;
		}else{
			return false;
		}
	}

	// 重写delete
	public boolean delete(String id){
		RoleResource brand = dao.findById(id);
		//删除logo文件
		if (StrKit.notBlank(brand.getStr("logo"))){
			File logoFile = new File(JFinal.me().getServletContext().getRealPath(brand.getStr("logo")));
			if (logoFile.exists()) {
				logoFile.delete();
			}
		}		
		return brand.delete();
	}
}
